﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Text;
using System.Windows.Forms;
using DevComponents.DotNetBar;
using PMQLCHH.BUS;
using PMQLCHH.DAO;
using PMQLCHH.DTO;

namespace PMQLCHH.GUI
{
    public partial class DSHOADON : DevComponents.DotNetBar.Office2007Form
    {
        //Kha Nguyen - Giao Dien Hoa Don - 11/6/2013 - begin
        public DSHOADON()
        {
            InitializeComponent();
           
        }

        //events
        private void btn_TimKiem_Click(object sender, EventArgs e)
        {
            TimKiemHoaDon();
        }

        private void btn_LamMoi_Click(object sender, EventArgs e)
        {
            LamMoi();
        }

        private void btn_Them_Click(object sender, EventArgs e)
        {
            LAPHOADON frm_LapHoaDon = new LAPHOADON();
            frm_LapHoaDon.ShowDialog();
            LamMoi();
        }

        private void btn_thoat_Click(object sender, EventArgs e)
        {
            this.Close();
        }

        private void DSHOADON_Load(object sender, EventArgs e)
        {
            LamMoi();
        }
        private void txt_MaHoaDon_KeyUp(object sender, KeyEventArgs e)
        {
            if (e.KeyCode == Keys.Enter)
            {
                btn_TimKiem.PerformClick();
            }
        }

        private void cbb_TrangThai_SelectedIndexChanged(object sender, EventArgs e)
        {
            //btn_TimKiem.PerformClick();
        }

        //su kien click nut xoa hoac nut sua
        private void dtg_DanhSachMatHang_CellContentClick(object sender, DataGridViewCellEventArgs e)
        {
            int iCot = e.ColumnIndex;
            int iDong = e.RowIndex;
            if (iCot == 0)
            {
                DialogResult KQ = MessageBox.Show("Bạn có chắc muốn xóa! \nDữ liệu sau khi xóa không thể phục hồi. Tiếp tục xóa?", "Xóa", MessageBoxButtons.YesNo);
                if (KQ == DialogResult.Yes)
                {
                    if (!XoaHoaDon(iDong))
                    {
                        MessageBox.Show("Hóa đơn đã thanh toán thì không thể xóa!", "Thông Báo");
                    }
                    else
                    {
                        LamMoi();
                    }
                }
                
            }
            else
            {
                if (iCot == 1)
                {
                    SuaHoaDon(iDong);
                    LamMoi();
                }
            }
        }


        //methods
        /// <summary>
        /// Lam Moi Chuc Nang Tim Kiem
        /// </summary>
        ///

        public void LamMoi()
        {
            txt_MaHoaDon.Text = "";
            NapDenNgayLap();
            
            NapDanhSachTrangThai();
            NapTuNgayLap();
            NapDanhSachHoaDon();
        }

        //methods
        /// <summary>
        /// tim kiem Hoa Don
        /// </summary>
        ///
        public void TimKiemHoaDon()
        {
            string MaHD = "";
            int TrangThai = 2;
            string NgayLapTu = "";
            string NgayLapDen = "";
            MaHD = txt_MaHoaDon.Text;
            TrangThai = int.Parse(cbb_TrangThai.SelectedValue.ToString());
            //Giaohang = int.Parse(cbb_GiaoHang.SelectedValue.ToString());
            NgayLapTu = dtp_NgayLapTu.Value.Year + "/" + dtp_NgayLapTu.Value.Month + "/" + dtp_NgayLapTu.Value.Day;
            NgayLapDen = dtp_NgayLapDen.Value.Year + "/" + dtp_NgayLapDen.Value.Month + "/" + dtp_NgayLapDen.Value.Day;
            HoaDonBUS HDBus = new HoaDonBUS();
            dtg_DanhSachMatHang.DataSource = HDBus.TimKiemHoaDon(MaHD, NgayLapTu, NgayLapDen, TrangThai);
            foreach (DataGridViewRow dr in dtg_DanhSachMatHang.Rows)
            {
                if (String.Compare(dr.Cells["TrangThai"].Value.ToString(), "Đã Thanh Toán") == 0)
                {
                    dr.DefaultCellStyle.BackColor = Color.Gray;
                    //dr.DefaultCellStyle.SelectionBackColor = Color.Gray;
                }
                else
                {
                    string MaHD_ = dr.Cells["MaHoaDon"].Value.ToString();
                    DataTable dt = HDBus.LayDSMHChiTietHoaDon(MaHD_);
                    if (DaHetHang(dt))
                    {
                        dr.Cells["MaHoaDon"].Style.ForeColor = Color.Red;
                        dr.Cells["MaHoaDon"].Style.SelectionForeColor = Color.Red;
                    }
                }
            }
        }

        /// <summary>
        /// Nap Danh Sach Hoa Don
        /// </summary>
        ///

        public void NapDanhSachHoaDon()
        {
            HoaDonBUS HDBus = new HoaDonBUS();
            dtg_DanhSachMatHang.DataSource = HDBus.LayDanhSachHoaDon();

            foreach (DataGridViewRow dr in dtg_DanhSachMatHang.Rows)
            {
                if (String.Compare(dr.Cells["TrangThai"].Value.ToString(), "Đã Thanh Toán") == 0)
                {
                    dr.DefaultCellStyle.BackColor = Color.Gray;
                    //dr.DefaultCellStyle.SelectionBackColor = Color.Gray;
                }
                else
                {
                    string MaHD = dr.Cells["MaHoaDon"].Value.ToString();
                    DataTable dt = HDBus.LayDSMHChiTietHoaDon(MaHD);
                    if (DaHetHang(dt))
                    {
                        dr.Cells["MaHoaDon"].Style.ForeColor = Color.Red;
                        dr.Cells["MaHoaDon"].Style.SelectionForeColor = Color.Red;
                    }
                }
            }
        }

        /// <summary>
        /// Xoa Hoa Don
        /// </summary>
        ///

        public bool XoaHoaDon(int iDong)
        {
            HoaDonBUS HDBus = new HoaDonBUS();
            string MaHD = dtg_DanhSachMatHang.Rows[iDong].Cells["MaHoaDon"].Value.ToString();
            return HDBus.XoaHoaHoaDon(MaHD);
        }

        /// <summary>
        /// Sua Hoa Don
        /// </summary>
        ///

        public void SuaHoaDon(int iDong)
        {
            HoaDonBUS HDBus = new HoaDonBUS();
            string MaHD = dtg_DanhSachMatHang.Rows[iDong].Cells["MaHoaDon"].Value.ToString();

            CHITIETHOADON frm_CTHD = new CHITIETHOADON(MaHD);
            frm_CTHD.ShowDialog();
        }

        /// <summary>
        /// Nap Ngay Lap (den ngay)
        /// </summary>
        ///


        public void NapDenNgayLap()
        {
            dtp_NgayLapDen.Format = DateTimePickerFormat.Custom;
            dtp_NgayLapDen.CustomFormat = "dd/MM/yyyy";
            dtp_NgayLapDen.Value = DateTime.Now;
        }

        /// <summary>
        /// Nap Ngay Lap (Tu ngay)
        /// </summary>
        ///


        public void NapTuNgayLap()
        {
            HoaDonBUS HDBus = new HoaDonBUS();
            dtp_NgayLapTu.Format = DateTimePickerFormat.Custom;
            dtp_NgayLapTu.CustomFormat = "dd/MM/yyyy";
            //dtp_NgayLapTu.Value = DateTime.Parse(HDBus.LayMaNgayLapHoaDonCuNhat());
        }

        /// <summary>
        /// Nap Trang Thai
        /// </summary>
        ///
        public void NapDanhSachTrangThai()
        {
            DataTable dt = new DataTable();
            dt.Columns.Add("Text");
            dt.Columns.Add("Value");

            //them gia tri
            DataRow newCustomersRow = dt.NewRow();
            newCustomersRow["Text"] = "Tất Cả";
            newCustomersRow["Value"] = 2;
            dt.Rows.Add(newCustomersRow);

            newCustomersRow = dt.NewRow();
            newCustomersRow["Text"] = "Chưa Thanh Toán";
            newCustomersRow["Value"] = 0;
            dt.Rows.Add(newCustomersRow);

            newCustomersRow = dt.NewRow();
            newCustomersRow["Text"] = "Đã Thanh Toán";
            newCustomersRow["Value"] = 1;
            dt.Rows.Add(newCustomersRow);

            cbb_TrangThai.DataSource = dt;
            cbb_TrangThai.ValueMember = "Value";
            cbb_TrangThai.DisplayMember = "Text";
            cbb_TrangThai.SelectedIndex = 0;
        }



        /// <summary>
        /// Kiem tra xem con hang hay khong
        /// </summary>
        ///

        public bool DaHetHang(DataTable DanhSachMatHang)
        {
            DataView dv = DanhSachMatHang.DefaultView;
            dv.Sort = "MaMatHang ASC";
            DanhSachMatHang = dv.ToTable();
            int Count = 0;
            bool KQ = false;
            string MaMatHangCu = "";
            MatHangBUS MHBus = new MatHangBUS();
            DataTable dt = new DataTable();

            foreach (DataRow dr in DanhSachMatHang.Rows)
            {
                string Temp = dr["MaMatHang"].ToString();
                int SoLuongMua = int.Parse(dr["SoLuong"].ToString());
                if (String.Compare(MaMatHangCu, Temp) == 0)
                {
                    Count += SoLuongMua;
                }
                else
                {
                    MaMatHangCu = Temp;
                    Count = SoLuongMua;
                }
                dt = MHBus.LayChiTietMatHang(MaMatHangCu);
                int SoLuongTon = 0;
                if (dt.Rows.Count != 0)
                {
                    SoLuongTon = int.Parse(dt.Rows[0]["SoLuongTon"].ToString());
                }
                if (SoLuongTon < Count)
                {
                        KQ = true;
                        break;
                }
            }
            return KQ;
        }

 

        //Kha Nguyen - Giao Dien Hoa Don - 11/6/2013 - end
    }

}